#!/bin/bash

. base.sh

ROS_PKG_SRC=${OUTPUT}/ros-pkg-src.list
ROS_PKG_LIST=${OUTPUT}/ros-pkg.list

prepare()
{
        if [ "${ROS_SRC_BASE}" = "" ]
        then
                error_log "Please give the source repo path of ros"
                exit 1
        fi

        if [ ! -d ${ROS_SRC_BASE} ]
        then
                error_log "Please give the source repo path of ros"
                exit 1
        fi

        if [ ! -f ${ROS_PKG_SRC} -o ! -f ${ROS_PKG_LIST} ]
        then
                error_log "Can not find ${ROS_PKG_SRC}, you can use get-repo-src.sh to create it"
                exit 1
        fi

        rm -rf ${ROS_REPO_BASE}
        mkdir -p ${ROS_REPO_BASE}
}

spec_fix()
{
	pkg=$1
	fix_type=$2
	fix_fake_str=$3
	spec=$4

	fix_file=${ROOT}/spec_fix/$pkg.$fix_type

	if [ -f $fix_file ]
	then
		for dep in `grep "^\-" $fix_file | sed -e "s#^\-##g"`
		do
			sed -i "/^$fix_type: *$dep/d" $spec
		done

		grep "^\+" $fix_file | sed -e "s#^\+#$fix_type: #g" >${OUTPUT}/.tempRequires

		n=`cat ${OUTPUT}/.tempRequires | wc -l`
		if [ $n -eq 0 ]
		then
                	sed -i '/$fix_fake_str/d' $spec
			return
		fi

                pkgRequires=`cat ${OUTPUT}/.tempRequires | sed ":a;N;s/\n/ROS_DESC_CRLF/g;ta"`
                sed -i "s#$fix_fake_str#$pkgRequires#g" $spec
                sed -i ":a;N;s/ROS_DESC_CRLF/\n/g;ta" $spec
	else
                sed -i "/$fix_fake_str/d" $spec
	fi
}

modify_spec()
{
        pkg=$1
        spec=$2
	pkg_dir_name=$3
        base_version=$4
        release_version=$5

        info_log "gen spec for $pkg"

        debug_log "gen version"

	csrc=`find $pkg_dir_name -name "*.c" | grep -v /test/ | wc -l`
	cppsrc=`find $pkg_dir_name -name "*.cpp" | grep -v /test/ | wc -l`

	if [ "$csrc" == "0" -a "$cppsrc" == "0" ]
	then
                sed -i "s#ROS_PACKAGE_NO_DEBUGINFO#%global debug_package %{nil}#g" $spec
	else
                sed -i '/ROS_PACKAGE_NO_DEBUGINFO/d' $spec
	fi


        sed -i "s#ROS_PACKAGE_NAME#$pkg#g" $spec
        sed -i "s#ROS_PACKAGE_VERSION#$base_version#g" $spec
        sed -i "s#ROS_PACKAGE_RELEASE#$release_version#g" $spec

        debug_log "gen desc"
        desc_wc=`cat ${ROS_DEPS_BASE}/$pkg-PackageXml-description | wc -l`
        if [ "$desc_wc" = "1" ]
        then
                desc=`cat ${ROS_DEPS_BASE}/$pkg-PackageXml-description`
                sed -i "s#ROS_PACKAGE_SUMMARY#$desc#g" $spec
                sed -i "s#ROS_PACKAGE_DESCRIPTION#$desc#g" $spec
        else
                desc=`cat ${ROS_DEPS_BASE}/$pkg-PackageXml-description | sed ":a;N;s/\n/ROS_DESC_CRLF/g;ta"`
                sed -i "s#ROS_PACKAGE_SUMMARY#ROS $pkg package#g" $spec
                sed -i "s^ROS_PACKAGE_DESCRIPTION^$desc^g" $spec
                sed -i ":a;N;s/ROS_DESC_CRLF/\n/g;ta" $spec
        fi

        debug_log "gen license"
        license_wc=`grep license: ${ROS_DEPS_BASE}/$pkg-PackageXml | awk -F"license:" '{print $2}' | wc -l`
        if [ "$license_wc" = "1" ]
        then
                license=`grep license: ${ROS_DEPS_BASE}/$pkg-PackageXml | awk -F"license:" '{print $2}'`
                sed -i "s#ROS_PACKAGE_LICENSE#$license#g" $spec
        else
                license=`grep license: ${ROS_DEPS_BASE}/$pkg-PackageXml | awk -F"license:" '{print $2}' | sed ":a;N;s/\n/ and /g;ta"`
                sed -i "s#ROS_PACKAGE_LICENSE#$license#g" $spec
        fi

        debug_log "gen url"
        url=`grep url: ${ROS_DEPS_BASE}/$pkg-PackageXml | awk -F"url:" '{print $2}' | sed -n '1p'`
        if [ "$url" = "" ]
        then
                sed -i '/ROS_PACKAGE_URL/d' $spec
        else
                sed -i "s#ROS_PACKAGE_URL#$url#g" $spec
        fi

        debug_log "gen Requires"
        if [ -f ${ROS_DEPS_BASE}/$pkg-Requires ]
        then
                pkgRequires_wc=`cat ${ROS_DEPS_BASE}/$pkg-Requires | wc -l`
                if [ "$pkgRequires_wc" = "1" ]
                then
                        pkgRequires=`cat ${ROS_DEPS_BASE}/$pkg-Requires`
                        sed -i "s#ROS_PACKAGE_REQUIRES#$pkgRequires#g" $spec
                else
                        pkgRequires=`cat ${ROS_DEPS_BASE}/$pkg-Requires | sed ":a;N;s/\n/ROS_DESC_CRLF/g;ta"`
                        sed -i "s#ROS_PACKAGE_REQUIRES#$pkgRequires#g" $spec
                        sed -i ":a;N;s/ROS_DESC_CRLF/\n/g;ta" $spec
                fi
        else
                sed -i '/ROS_PACKAGE_REQUIRES/d' $spec
        fi

        debug_log "gen BuildRequires"
        if [ -f ${ROS_DEPS_BASE}/$pkg-BuildRequires ]
        then
                pkgRequires_wc=`cat ${ROS_DEPS_BASE}/$pkg-BuildRequires | wc -l`
                if [ "$pkgRequires_wc" = "1" ]
                then
                        pkgRequires=`cat ${ROS_DEPS_BASE}/$pkg-BuildRequires`
                        sed -i "s#ROS_PACKAGE_BUILDREQUIRES#$pkgRequires#g" $spec
                else
                        pkgRequires=`cat ${ROS_DEPS_BASE}/$pkg-BuildRequires | sed ":a;N;s/\n/ROS_DESC_CRLF/g;ta"`
                        sed -i "s#ROS_PACKAGE_BUILDREQUIRES#$pkgRequires#g" $spec
                        sed -i ":a;N;s/ROS_DESC_CRLF/\n/g;ta" $spec
                fi
        else
                sed -i '/ROS_PACKAGE_BUILDREQUIRES/d' $spec
        fi

        debug_log "gen test BuildRequires"
        if [ -f ${ROS_DEPS_BASE}/$pkg-test-BuildRequires ]
        then
                pkgRequires_wc=`cat ${ROS_DEPS_BASE}/$pkg-test-BuildRequires | wc -l`
                if [ "$pkgRequires_wc" = "1" ]
                then
                        pkgRequires=`cat ${ROS_DEPS_BASE}/$pkg-test-BuildRequires`
                        sed -i "s#ROS_TEST_BUILDREQUIRES#$pkgRequires#g" $spec
                else
                        pkgRequires=`cat ${ROS_DEPS_BASE}/$pkg-test-BuildRequires | sed ":a;N;s/\n/ROS_DESC_CRLF/g;ta"`
                        sed -i "s#ROS_TEST_BUILDREQUIRES#$pkgRequires#g" $spec
                        sed -i ":a;N;s/ROS_DESC_CRLF/\n/g;ta" $spec
                fi
        else
                sed -i '/ROS_TEST_BUILDREQUIRES/d' $spec
        fi

	if [ "$pkg" == "ament-cmake-core" -o "$pkg" == "ament-package" -o "$pkg" == "ros-workspace" ]
	then
                sed -i '/ROS_ALL_FIX_REQUIRES/d' $spec
	else
                sed -i "s#ROS_ALL_FIX_REQUIRES##g" $spec
	fi

	spec_fix $pkg Requires ROS_FIX_REQUIRES $spec
	spec_fix $pkg BuildRequires ROS_FIX_BUILDREQUIRES $spec

        debug_log "gen changelog"
        maintainer=`grep maintainer: ${ROS_DEPS_BASE}/$pkg-PackageXml | awk -F"maintainer:" '{print $2}'`
        #changetime=`date +"%a %b %d %Y"`
        changetime=`date +"%a %b %Y"`
        changelog="$changetime $maintainer - $base_version-$release_version"
        sed -i "s#ROS_PACKAGE_CHANGELOG#$changelog#g" $spec

        debug_log "gen spec ok"
}

main()
{
        prepare

        info_log "Start to analyse ros-pkg."

        for repo in `cat ${ROS_PKG_LIST} | awk '{print $2}' | sort | uniq`
        do

                #if [ "$repo" != "control_box_rst" ]
                #then
                #       continue
                #fi

                info_log "start to gen $repo"

                mkdir -p ${ROS_REPO_BASE}/${repo}/
                cd ${ROS_REPO_BASE}/${repo}/

                grep -P "\t$repo\t" ${ROS_PKG_SRC} >${OUTPUT}/.repo_pkgs
                grep -P "\t$repo/" ${ROS_PKG_SRC} >>${OUTPUT}/.repo_pkgs

                echo "<multibuild>" >_multibuild
                while read pkg path version
                do
                        if [ "$pkg" = "" -o "$path" = "" -o "$version" = "" ]
                        then
                                error_log "Wrong package $pkg"
                                exit 1
                        fi

                        base_version=`echo $version | awk -F"-" '{print $1}'`
                        release_version=`echo $version | awk -F"-" '{print $2}'`

			if [ "$SRC_TAR_FROM" == "ubuntu" ]
			then
                		pkg_dir_name=`cd ${ROS_SRC_BASE}/${repo} && ls ros-${ROS_DISTRO}-${pkg}_*.orig.tar.gz | sed -e "s#.orig.tar.gz##g" | sed -e "s#_#-#g"`
                		pkg_tar=`cd ${ROS_SRC_BASE}/${repo} && ls ros-${ROS_DISTRO}-${pkg}_*.orig.tar.gz`
				cp ${ROS_SRC_BASE}/${repo}/$pkg_tar ${ROS_REPO_BASE}/$repo/
                        	if [ -f ${ROS_SRC_BASE}/${repo}/${pkg_dir_name}/setup.py ]
                        	then
                        	        cp ${ROOT}/template/py-ubuntu.spec $pkg.spec
                        	else
                        	        cp ${ROOT}/template/cmake-ubuntu.spec $pkg.spec
                        	fi

				pkg_dir_name=${ROS_SRC_BASE}/${repo}/$pkg_dir_name
			else
				pkg_dir_name=$pkg-$base_version
                        	mkdir -p $pkg_dir_name
                        	cp -r ${ROS_SRC_BASE}/$path/* $pkg-$base_version/
                        	tar -czf $pkg-$base_version.tar.gz $pkg-$base_version

                        	if [ -f $pkg-$base_version/setup.py ]
                        	then
                        	        cp ${ROOT}/template/py.spec $pkg.spec
                        	else
                        	        cp ${ROOT}/template/cmake.spec $pkg.spec
                        	fi
			fi

                        modify_spec $pkg $pkg.spec $pkg_dir_name $base_version $release_version

                        echo -e "\t<flavor>$pkg</flavor>" >>_multibuild
                done < ${OUTPUT}/.repo_pkgs

                echo "</multibuild>" >>_multibuild
        done

        info_log "Gen ros-pkg-src.list done, you can find it in ${ROS_PKG_SRC}"
}

main $*
